#ifndef __TORRENT_H__
#define __TORRENT_H__

#include <sys/types.h>

#include "xx.h"
#include "tools.h"
#include "list.h"
//#include "peer.h"

#define  PEER_MAX		1000

enum state{
	STAT_IDLE = 1,
	STAT_DOWNLOADING,
	STAT_STOPPED,
	STAT_CLOSED,
	STAT_DONE
};

struct torrent{
	char				* torrent_path[MAX_PATH_LEN];

	xx_metainfo			* metainfo;
	xx_tracker			* tracker;
	list_node			* peer_list;
	fd_set				  peer_fds;
	
	int					  peer_num;

	int					  listen_port;
	int					  listen_fd;

	char				* announc;
	char				  info_hash[20 * 3 + 1];
	char				  peer_id[20 + 1];

	xx_state			  state;
	char				  event[10];

	unsigned int		  bitfiled;

	int					  uploaded;
	int					  downloaded;
	int					  left;

	int					  complete;
	int					  incomplete;

};

int xx_load_torrent(const char *filename, xx_metainfo *meta);

int	xx_torrent_init(char *torrentPath, xx_torrent *torrent);

void xx_torrent_start(xx_torrent *torrent);
#endif
